<?php
/**
 * ExaminationType Service.
 * User: Administrator
 * Date: 2020/2/13 0013
 * Time: 下午 2:02
 */
namespace app\api\service\questions;

use app\api\model\questions\Question as QuestionModels;
use app\api\model\questions\Answer;

Class Question {

    /**
     * 根据类型获取考试类型
     * @param int $type
     * @return mixed
     */
    public function getType( int $type )
    {
        return QuestionModels::with(['examination_type','examination_section'])->type($type)->select();
    }

    /**
     * 获取题目
     *
     * @return mixed
     */
    public function detail( int $id )
    {
        return QuestionModels::with(['answer'=>function( $query ){
            $query->with('opt')->append(['type_text']);
        }])->getOrFail($id);
    }

    /**
     * 根据考试类别获取题目
     *
     * @param array $data
     * @return mixed
     */
    public function category(int $category, array $data)
    {
        $questions = QuestionModels::with(['doAnswer'])->category($category)->where('examination_type', $data['examination_type'])->select()->toArray();
        foreach ( $questions as $key => $_question ) {
            $aid = [];
            foreach ( $_question['do_answer'] as $_do_answer ) {
                if ( !in_array($_do_answer['aid'], $aid) ) {
                    $aid[] = $_do_answer['aid'];
                }
            }
            $questions[$key]['done'] = count($aid);
            $questions[$key]['count'] = Answer::where('qid', $_question['id'])->count();
        }
        return $questions;
    }

}